<template>
  <div id="app">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>宠物详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="medium"
                 @click="$router.push('/sys-admin/pet-management/add-new')">添加宠物
      </el-button>
    </div>
      <!--宠物列表开始-->
      <el-table :data="petArr" border style="width: 100%">
        <h1>领养审核</h1>
        <el-divider></el-divider>
        <el-table-column label="序号" type="index" width="60px"></el-table-column>
        <el-table-column prop="petName" label="名称" width="80px"></el-table-column>
        <el-table-column prop="petType" label="类型" width="120px"></el-table-column>
        <el-table-column prop="sex" label="性别" width="120px"></el-table-column>
        <el-table-column prop="birthday" label="生日" width="160px"></el-table-column>
        <el-table-column prop="state" label="状态" width="90px"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="查看图片" width="120" align="center">
          <template slot-scope="scope" width="120px">
            <el-button @click="gotoPetPic(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" circle
                       @click="handleProductUpdate(scope.row)"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" circle
                       @click="handleProductDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页控件 -->
      <div style="margin: 10px 0; text-align: right;">
        <el-pagination
            @current-change="changePage"
            :hide-on-single-page="true"
            :page-size="pageSize"
            :current-page="currentPage"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
      <!--领养审核结束-->
  </div>
</template>

<script>
export default {
  data() {
    return {
      pet: {},
      input:'',
      // 分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      pageSize: 20,
      pageCount: 1,
      total: 0,
      data: '',
      petArr: [],
    }
  },
  methods: {
    //跳转到宠物图片
    gotoPetPic(){
      this.$router.push('/sys-admin/pet-management/picture?petId=' + this.pet.id);
    },
    //修改宠物信息
    handleProductUpdate(){

    },
    //删除宠物信息
    handleProductDelete(){
      let url = 'http://localhost:13001/pet/delete?id=' + this.pet.id;
      console.log('url = ' + url);

      let localJwt = JSON.parse(localStorage.getItem('localJwt'));

      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除'+jsonResult.data.petName+'成功！',
            type: 'success'
          });
          this.loadPetList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadPetList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 切换分页
    changePage(page) {
      this.$router.replace('?page=' + page);
      console.log("page:"+page);
      this.loadPetList();
    },
    //领养宠物
    handlePetAdopt(){

    },
    //加载宠物列表
    loadPetList(){
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }
      let url = 'http://localhost:13001/pet/list?pageNum=' + page;
      console.log('url = ' + url);
      let localJwt = JSON.parse(localStorage.getItem('localJwt'));
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt.token
            }
          })
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.petArr = jsonResult.data.list;
          console.log(this.petArr);
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.pageCount = jsonResult.data.maxPage;
          this.total = jsonResult.data.total;
        }
      });
    }
  },
  mounted() {
    this.loadPetList();
  }
}
</script>

<style scoped>

</style>